/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.farent.solutions.plugins.stripes.hyperstripes.core.tags;

import com.farent.solutions.plugins.stripes.hyperstripes.core.ajax.AjaxResponseInsert;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentProcessor;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentTagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormChildComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormMode;
import com.farent.solutions.plugins.stripes.hyperstripes.core.PrettyBuilder;
import com.farent.solutions.plugins.stripes.hyperstripes.utils.Utils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

/**
 *
 * @author frenio
 */
public class StandardHTMLTagHandler extends AbstractComponentTagHandler
        implements ComponentTagHandler{

    public String processTemplate(InputStream template, Object bindings, Map var) throws UnsupportedEncodingException {
        CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);

        String out = (String) TemplateRuntime.execute(compiledTemplate, bindings, var);
        if( out.contains("@{")){
            InputStream is = new ByteArrayInputStream(out.getBytes("UTF-8"));
            out = processTemplate(is, bindings, var);
        }
        return out;
    }

    @Override
    public String createContent(ComponentModel model, Object bindings) {
        String content = "";
        try {
            StandardHTMLComponentModel htmlModel = (StandardHTMLComponentModel) model;
            List<ComponentModel> children = htmlModel.getChildren();
            StringBuilder prettyBuilder = new StringBuilder();
            Map var = new HashMap();
            var.put("model", htmlModel);
            String name = TagUtils.getNameNoPrefix(htmlModel.getName());
            var.put("tagName", name);
            if ( children.size() > 0 ) {
                for (int i = 0; i < children.size(); i++) {
                    FormChildComponentModel childModel = (FormChildComponentModel) children.get(i);
                    childModel.setMode(htmlModel.getMode());
                    String childString = ComponentProcessor.processComponentContent(childModel, bindings);
                    prettyBuilder.append(childString);
                }
                var.put("content", prettyBuilder.toString());
            } else if(htmlModel.getText() != null){
                var.put("content", htmlModel.getText());
            }
            else
            {
                var.put("content", Utils.EMPTY_STRING);
            }
            InputStream template3 = InputTagHandler.class.getResourceAsStream("standardHtmlElement.tag");
            content = processTemplate(template3, bindings, var);

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StandardHTMLTagHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return content;
    }

    @Override
    public void createContent(ComponentModel model, Object bindings, OutputStream outputStream) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
